package com.woniu108.car.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.car.param.*;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.dto.CarHotDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.woniu108.car.form.UpdateCarHotForm;
import com.woniu108.car.model.CarHot;
import com.woniu108.car.service.CarHotService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 车辆品牌热度表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/carHot")
@Api(tags = "车辆品牌热度接口")
public class CarHotController {
    @Resource
    private CarHotService carHotService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 查询热度榜所有未删除品牌信息
     * @return CarHot集合
     */
    @GetMapping("/findAllCarHot.api")
    @ApiOperation("查询热度榜所有未删除品牌信息")
    public Result<List<CarHotDto>> findAllCarHot() {
        List<CarHot> list = carHotService.findAllCarHot();
        List<CarHotDto> dtos = BeanUtil.copyToList(list, CarHotDto.class);
        return Result.newListInstance(CarHotDto.class).setData(dtos).setMsg("查询热度榜所有未删除品牌信息成功");
    }
    /**
     * 查询热度榜所有正常品牌信息
     * @return CarHot集合
     */
    @GetMapping("/findAllNormalCarHot")
    @ApiOperation("查询热度榜所有正常品牌信息")
    public Result<List<CarHotDto>> findAllNormalCarHot() {
        List<CarHot> list = carHotService.findAllNormalCarHot();
        List<CarHotDto> dtos = BeanUtil.copyToList(list, CarHotDto.class);
        return Result.newListInstance(CarHotDto.class).setData(dtos).setMsg("查询热度榜所有正常品牌信息成功");
    }
    /**
     * 新增某品牌汽车热度榜
     * @param brandId 品牌id
     * @return boolean值
     */
    @GetMapping("/insertCarHot.api")
    @ApiOperation("新增某品牌汽车热度榜")@AdminHistoryLog
    @ApiImplicitParam(name = "brandId",value = "品牌id")
    public Result<Boolean> insertCarHot(Integer brandId, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        InsertCarHotParam param = new InsertCarHotParam(brandId, adminId);

        Boolean bool = carHotService.insertCarHot(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("新增汽车热度榜成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("新增汽车热度榜失败").setData(false).setCode(500);
    }
    /**
     * 删除某品牌热度榜
     * @param brandIds 品牌id集合
     * @return boolean值
     */
    @PostMapping("/deleteCarHot.api")
    @ApiOperation("删除某品牌热度榜")@AdminHistoryLog
    @ApiImplicitParam(name = "brandIds",value = "品牌id集合")
    public Result<Boolean> deleteCarHot(@RequestBody List<Integer> brandIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        DeleteCarHotParam param = new DeleteCarHotParam(brandIds, adminId);
        Boolean bool = carHotService.deleteCarHot(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("删除汽车热度榜成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("删除汽车热度榜失败").setData(false).setCode(500);
    }
    /**
     * 禁用某品牌热度榜
     * @param brandIds 品牌id集合
     * @return boolean值
     */
    @PostMapping("/banCarHot.api")
    @ApiOperation("禁用某品牌热度榜")@AdminHistoryLog
    @ApiImplicitParam(name = "brandIds",value = "品牌id集合")
    public Result<Boolean> banCarHot(@RequestBody List<Integer> brandIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        BanCarHotParam param = new BanCarHotParam(brandIds, adminId);
        Boolean bool = carHotService.banCarHot(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("禁用汽车热度榜成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("禁用汽车热度榜失败").setData(false).setCode(500);
    }
    /**
     * 解锁某品牌热度榜
     * @param brandIds 品牌id集合
     * @return boolean值
     */
    @PostMapping("/unlockCarHot.api")
    @ApiOperation("解锁某品牌热度榜")@AdminHistoryLog
    @ApiImplicitParam(name = "brandIds",value = "品牌id集合")
    public Result<Boolean> unlockCarHot(@RequestBody List<Integer> brandIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UnlockCarHotParam param = new UnlockCarHotParam(brandIds, adminId);
        Boolean bool = carHotService.unlockCarHot(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("解锁汽车热度榜成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("解锁汽车热度榜失败").setData(false).setCode(500);
    }
    /**
     * 修改某品牌热度榜
     * @param form 品牌id，热度值，
     * @return boolean值
     */
    @PostMapping("/updateCarHot.api")
    @ApiOperation("修改某热度榜信息")@AdminHistoryLog
    public Result<Boolean> updateCarHot(@RequestBody UpdateCarHotForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UpdateCarHotParam param = BeanUtil.toBean(form, UpdateCarHotParam.class);
        param.setAdminId(adminId);
        Boolean bool = carHotService.updateCarHot(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("修改汽车热度榜成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("修改汽车热度榜失败").setData(false).setCode(500);

    }
}

